package com.zac.coroutine.algorithm.leetcode.array

/**
 * author zac
 * date 2024/10/11
 * --简单--
 * 已知一个数组有1000个数,取值范围为[0,999],其中有且仅有两个数相同,找到这个数
 *
 */
class Test1 {


    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            findSameNum(generateArr())
        }

        private fun generateArr(): IntArray {
            val numbers = (0..999).toList()
            val shuffledNumbers = numbers.shuffled().toIntArray()
            shuffledNumbers[998] = shuffledNumbers[888]
            shuffledNumbers.forEach {
                print("$it,")
            }

            print("\n第888个数字为:${shuffledNumbers[888]}")
            print("\n第998个数字为:${shuffledNumbers[998]}")


            return shuffledNumbers
        }

        private fun findSameNum(array: IntArray) {
            val newArr = Array(1000) { -1 }

            for (i in 0 until 1000) {
                if (newArr[array[i]] == 0) {
                    newArr[array[i]] = i
                } else {
                    print("相同的数字为${array[i]}")
                    break
                }
            }
        }
    }
}